home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Magazine / Online / httpproxy / src / logging.c < prev    next >
C/C++ Source or Header  |  1996-08-20  |  7KB  |  316 lines

  1. /*(( "Header" */
  2. /*
  3.  * $Id: logging.c,v 1.6 1996/08/12 03:33:36 mshopf Exp mshopf $
  4.  *
  5.  * (c) 1995-96 Matthias Hopf
  6.  *
  7.  * Log file functions for httpproxy.
  8.  *
  9.  */
  10.  
  11. /*
  12.  * $Log: logging.c,v $
  13.  * Revision 1.6  1996/08/12  03:33:36  mshopf
  14.  * logging Url from extra field of request_t now.
  15.  *
  16.  * Revision 1.5  1996/08/11  22:25:15  mshopf
  17.  * reworked debug messages.
  18.  *
  19.  * Revision 1.4  1996/07/30  13:57:03  mshopf
  20.  * output alignment fix.
  21.  *
  22.  * Revision 1.3  1996/07/17  16:42:42  mshopf
  23.  * added IoErr() error messages.
  24.  *
  25.  * Revision 1.2  1996/04/26  05:14:03  mshopf
  26.  * V0.13 alpha 5 fix.
  27.  *
  28.  * Revision 1.1  1996/04/24  03:20:13  mshopf
  29.  * Initial revision
  30.  *
  31.  */
  32.  
  33.  
  34. /*)) */
  35. /*(( "Logfile format" */
  36.  
  37. /* Format of the logfile in EBNF:
  38.  *
  39.  * Logfile       ::= { LogEntry }*
  40.  * LogEntry      ::= { Special | Standard | Message | Error | WarnInfo } '\n'
  41.  *
  42.  * Special       ::= '*'   AnyText
  43.  * Standard      ::= ' '   Connection Sp StdConnect
  44.  * Message       ::= '.'   Connection Sp MsgConnect
  45.  * Error         ::= '-' { Connection Sp ErrConnect | ErrNoConnect }
  46.  * WarnInfo      ::= '+' { Connection Sp WnIConnect | WnINoConnect }
  47.  *
  48.  * Connection    ::= '#' Slot Sp HostAddr
  49.  * StdConnect    ::= Status Sp [ Url ]
  50.  * MsgConnect    ::= 'message' Sp [ Url ] Sp ' : ' InfoText
  51.  * ErrConnect    ::= { 'error' | 'errmsg' } Sp [ Url ] Sp ' : ' InfoText
  52.  * WnIConnect    ::= { 'warning' | 'info' } Sp [ Url ] Sp ' : ' InfoText
  53.  * ErrNoConnect  ::= 'error : ' InfoText
  54.  * WnINoConnect  ::= { 'warning' | 'info' } Sp ' : ' InfoText
  55.  *
  56.  * Slot          ::= Number
  57.  * HostAddr      ::= { INetAddr ':' Port } | ':proxy:'
  58.  * Status        ::= 'cached' | 'new' | 'proxied' | 'queued'
  59.  * Url           ::= 'URL=' AnyNoSpText
  60.  * InfoText      ::= Reason [':' Errno]
  61.  *
  62.  * Port          ::= Number
  63.  * Reason        ::= AnyNoCoText
  64.  * Errno         ::= AnyText
  65.  *
  66.  * Partial semi-formal description of all low-level symbols:
  67.  *
  68.  * Number        ::= { nummeric }+
  69.  * INetAddr      ::= { alphanummeric | '.' }+
  70.  * AnyNoSpText   ::= { printable }*
  71.  * AnyNoCoText   ::= { printable w/o ':'  |  ' ' }*
  72.  * AnyText       ::= { printable | ' ' }*
  73.  * Sp            ::= { ' ' }*
  74.  */
  75.  
  76. /*)) */
  77. /*(( "Includes/Constants" */
  78.  
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <stddef.h>
  82. #include <stdarg.h>
  83. #include <errno.h>
  84. #include <string.h>
  85. #include <unistd.h>
  86.  
  87. #include "httpproxy.h"
  88. #include "logging.h"
  89.  
  90. /*)) */
  91. /*(( "Global Variables" */
  92.  
  93. static FILE *LogStream = NULL;
  94.  
  95. /*)) */
  96. /*(( "assert()" */
  97.  
  98. #ifndef NDEBUG
  99.  
  100. void ASSERT (int x, const char *text, const char *file, const char *func, int line)
  101. {
  102.     extern FILE *LogStream;
  103.     if (x)
  104.     return;
  105.     if (! text)
  106.     text = "unknown";
  107.     fprintf (stderr, "assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line);
  108.     debugraw (D_ALWAYS, ("assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line));
  109.     if (LogStream)
  110.     {
  111.     fprintf (LogStream, "*assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line);
  112.     fclose  (LogStream);
  113.     LogStream = NULL;
  114.     }
  115.     ExitAll (20);
  116.     /*NOTREACHED*/
  117. }
  118.  
  119. #endif /* NDEBUG */
  120.  
  121. /*)) */
  122. /*(( "LogOpenFile()" */
  123.  
  124. void LogOpenFile (const char *Name)
  125. {
  126.     if (LogStream)
  127.     LogCloseFile ();
  128.     if (! (LogStream = fopen (Name, "a+")))
  129.     {
  130.     fprintf (stderr, "cannot open logfile '%s': %s\n", Name, strerror (errno));
  131.     ExitAll (20);
  132.     }
  133. }
  134.  
  135. /*)) */
  136. /*(( "LogCloseFile()" */
  137.  
  138. void LogCloseFile (void)
  139. {
  140.     if (LogStream)
  141.     fclose (LogStream);
  142.     LogStream = NULL;
  143. }
  144.  
  145. /*)) */
  146. /*(( "LogSpecial()" */
  147.  
  148. void LogSpecial (const char *Template, ...)
  149. {
  150.     va_list Args;
  151.  
  152.     assert (LogStream != NULL);
  153.     fputs ("*", LogStream);
  154.     debug (D_MSG, ("*"));
  155.  
  156.     va_start  (Args, Template);
  157.     vfprintf  (LogStream, Template, Args);
  158.     vdebugraw (D_MSG, Template, Args);
  159.     va_end    (Args);
  160.     fflush    (LogStream);
  161. }
  162.  
  163. /*)) */
  164. /*(( "LogStd()" */
  165.  
  166. void LogStd (request_t *Req, const char *Status)
  167. {
  168.     assert (Req != NULL);
  169.  
  170.     if (Req->Flags & REQ_LOGGED)
  171.     return;
  172.  
  173.     if (! Status)
  174.     {
  175.     LogErr (Req, L_ERROR, NULL, 0, "*** unknown error ***");
  176.     return;
  177.     }
  178.  
  179.     if (Req->Flags)
  180.     Req->Flags |= REQ_LOGGED;
  181.  
  182.     if (Req->Address[0])
  183.     {
  184.     fprintf (LogStream, " #%02d %-16s %-10s", Req-Requests, Req->Address, Status);
  185.     debug   (D_MSG, (   " #%02d %-16s %-10s", Req-Requests, Req->Address, Status));
  186.     }
  187.     else
  188.     {
  189.     fprintf (LogStream, " #%02d :proxy:          %-10s", Req-Requests, Status);
  190.     debug   (D_MSG, (   " #%02d :proxy:          %-10s", Req-Requests, Status));
  191.     }
  192.  
  193.     if (Req->Url [0])
  194.     {
  195.     fprintf (LogStream, " URL=%s\n", Req->Url);
  196.     debugraw (D_MSG, (  " URL=%s\n", Req->Url));
  197.     }
  198.     else
  199.     {
  200.     fputs  ("\n", LogStream);
  201.     debugraw (D_MSG, ("\n"));
  202.     }
  203.     fflush (LogStream);
  204. }
  205.  
  206. /*)) */
  207. /*(( "LogErr()" */
  208.  
  209. void LogErr (request_t *Req, const char *Status, const char *Url, int ErrNo, const char *Reason, ...)
  210. {
  211.     va_list Args;
  212.     char   IoErrTxt [128];
  213.     char   Type;
  214.  
  215.     assert (Reason != NULL);
  216.     va_start (Args, Reason);
  217.  
  218.     Type = '+';
  219.     if (Status == L_ERROR)
  220.     {
  221.     Status = "error";
  222.     Type   = '-';
  223.     }
  224.     if (Status == L_MSG)
  225.     {
  226.     Status = "message";
  227.     Type   = '.';
  228.     }
  229.     if (Status == L_ERRMSG)
  230.     {
  231.     Status = "errmsg";
  232.     Type   = '-';
  233.     }
  234.  
  235.     if (! Req)
  236.     {
  237.     fprintf (LogStream, "%c%-10s : ", Type, Status);
  238.     debug   (D_MSG, (   "%c%-10s : ", Type, Status));
  239.     vfprintf (LogStream, Reason, Args);
  240.     vdebugraw (D_MSG, Reason, Args);
  241.     if (ErrNo > 0)
  242.     {
  243.         fprintf (LogStream, ": %s\n", strerror (ErrNo));
  244.         debugraw (D_MSG, (  ": %s\n", strerror (ErrNo)));
  245.         errno = 0;
  246.     }
  247.     else if (ErrNo < 0)
  248.     {
  249.         Fault (IoErr (), "", IoErrTxt, 128);
  250.         fprintf (LogStream, "%s\n", IoErrTxt);
  251.         debugraw (D_MSG, (  "%s\n", IoErrTxt));
  252.     }
  253.     else
  254.     {
  255.         fputs  ("\n", LogStream);
  256.         debugraw (D_MSG, ("\n"));
  257.     }
  258.     }
  259.     else
  260.     {
  261.     if (Req->Flags)
  262.         Req->Flags |= REQ_LOGGED;
  263.  
  264.     if (Req->Address[0])
  265.     {
  266.         fprintf (LogStream, "%c#%02d %-16s %-10s", Type, Req-Requests, Req->Address, Status);
  267.         debug   (D_MSG, (   "%c#%02d %-16s %-10s", Type, Req-Requests, Req->Address, Status));
  268.     }
  269.     else
  270.     {
  271.         fprintf (LogStream, "%c#%02d :proxy:          %-10s", Type, Req-Requests, Status);
  272.         debug   (D_MSG, (   "%c#%02d :proxy:          %-10s", Type, Req-Requests, Status));
  273.     }
  274.  
  275.     if (Url)
  276.     {
  277.         fprintf (LogStream, " URL=%s : ", Url);
  278.         debugraw (D_MSG, (  " URL=%s : ", Url));
  279.     }
  280.     else if (Req->Url [0])
  281.     {
  282.         fprintf (LogStream, " URL=%s : ", Req->Url);
  283.         debugraw (D_MSG, (  " URL=%s : ", Req->Url));
  284.     }
  285.     else
  286.     {
  287.         fputs  (" : ", LogStream);
  288.         debugraw (D_MSG, (" : "));
  289.     }
  290.     vfprintf (LogStream, Reason, Args);
  291.     vdebugraw (D_MSG, Reason, Args);
  292.     if (ErrNo > 0)
  293.     {
  294.         fprintf (LogStream, ": %s\n", strerror (ErrNo));
  295.         debugraw (D_MSG, (  ": %s\n", strerror (ErrNo)));
  296.         errno = 0;
  297.     }
  298.     else if (ErrNo < 0)
  299.     {
  300.         Fault (IoErr (), "", IoErrTxt, 128);
  301.         fprintf (LogStream, "%s\n", IoErrTxt);
  302.         debugraw (D_MSG, (  "%s\n", IoErrTxt));
  303.     }
  304.     else
  305.     {
  306.         fputs  ("\n", LogStream);
  307.         debugraw (D_MSG, ("\n"));
  308.     }
  309.     }
  310.  
  311.     va_end (Args);
  312.     fflush (LogStream);
  313. }
  314.  
  315. /*)) */
  316.